package thread;

/**
 * 线程状态：
 * 1、new  新建状态
 * 2、runnable  运行状态
 * 3、timed_waiting 休眠状态
 * 4、terminated 终止状态
 * 5、waiting 等待
 * 6、blocked 阻塞
 */
public class ThreadState extends Thread {
    @Override
    public void run() {
        int i=0;
        while (true){
            i++;
            //if (i%1000==0)
            {
                try {
                    System.out.println("state********=>"+getState());
                    Thread.sleep(5000);
                } catch (Exception e) {
                    //e.printStackTrace();
                    System.out.println("休眠被中断了");
                }
            }
            if (i>20)break;
        }
    }
    public static void main(String[] agrs){
        ThreadState ts=new ThreadState();
        Thread th=new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    System.out.println("我是守护线程");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        th.setDaemon(true);//设置守护线程
        th.start();
        System.out.println("new=>"+ts.getState());
        ts.start();
        System.out.println("start=>"+ts.getState());
        while (true){
            if (ts.getState()==State.TIMED_WAITING){
                ts.interrupt();//中断休眠
            }


//            if (ts.isAlive()){//是否处于存活状态
//                System.out.println("main=>"+ts.getState());
//            }
//            else {
//                System.out.println("**=>"+ts.getState());//终止状态
//                break;
//            }
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }
    }
}
